/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package exatrez.stripes.action;

import able.stripes.JSP;
import com.google.inject.Inject;
import exatrez.dao.StatusRemarkCodeDAO;
import exatrez.manager.RemarkManager;
import exatrez.model.InfoAudit;
import exatrez.model.SeverityCode;
import exatrez.model.StatusRemarkCode;
import exatrez.model.User;
import exatrez.stripes.BaseActionBean;
import exatrez.stripes.config.AuthRequired;
import java.util.Date;
import java.util.List;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author wan.fairul
 */
@UrlBinding("/adminRemark")
@AuthRequired
public class StatusRemarkAction extends BaseActionBean {

    private static Logger LOG = LoggerFactory.getLogger(StatusRemarkAction.class);
    @Inject
    private RemarkManager remarkManager;
    private StatusRemarkCode remark;
    private List<StatusRemarkCode> listofStatusRemark;
    @Inject
    private StatusRemarkCodeDAO statusRemarkCodeDAO;

    @DefaultHandler
    public Resolution add_remark() {
        remark = new StatusRemarkCode();
        listofStatusRemark = remarkManager.findAllVesselGroup();
        return new JSP("admin/status_remark.jsp");
    }

    public Resolution saveStatusRemark() {
        StatusRemarkCode s = remarkManager.findRemarkCode(remark.getStatusRemarkName());
        LOG.debug("masuk");
        if (s == null) {
            User user = getContext().getCurrentUser();
            InfoAudit ia = new InfoAudit();
            ia.setCreateDate(new Date());
            ia.setCreateId(user);
            remark.setInfoAudit(ia);
            remarkManager.saveOrUpdate(remark);
            addSimpleMessage("Success");
            LOG.debug("finish");
        } else {
            addSimpleError("Duplicate Status Remark!");
        }
        return add_remark();
    }

    public Resolution deleteStatusRemark() {
        String statusRemarkCodeId = getContext().getRequest().getParameter("statusRemarkCodeId");
        if (statusRemarkCodeId != null) {
            remark = statusRemarkCodeDAO.findById(Integer.parseInt(statusRemarkCodeId));
            if (remark != null) {
                remarkManager.deleteSeverity(remark);
            }
        }
        return new RedirectResolution(StatusRemarkAction.class, "add_remark");
    }

    public Resolution editStatusRemark() {
        String statusRemarkCodeId = getContext().getRequest().getParameter("statusRemarkCodeId");
        if (statusRemarkCodeId != null) {
            remark = statusRemarkCodeDAO.findById(Integer.parseInt(statusRemarkCodeId));
        }
        return new JSP("admin/edit_status_remark.jsp").addParameter("popup", true);
    }

    public Resolution saveEditStatusRemark() {
        String statusRemarkCodeId = getContext().getRequest().getParameter("remark.statusRemarkCodeId");
        if (statusRemarkCodeId != null) {
            User user = getContext().getCurrentUser();
            StatusRemarkCode s = statusRemarkCodeDAO.findById(Integer.parseInt(statusRemarkCodeId));
             if (!remark.getStatusRemarkName().equals(s.getStatusRemarkName())) {
                StatusRemarkCode sc = remarkManager.findNotRemarkCode(String.valueOf(s.getStatusRemarkCodeId()), remark.getStatusRemarkName());
                if (sc != null) {
                    addSimpleError("Duplicate Status Remark Name!");
                    return add_remark() ;
                } else {
                    s.setStatusRemarkName(remark.getStatusRemarkName());
                }
            }
            InfoAudit ia = s.getInfoAudit();            
            ia.setUpdateDate(new Date());
            ia.setUpdateId(user);
            s.setInfoAudit(ia);
            remarkManager.saveOrUpdate(s);
            addSimpleMessage("Success Update");
        }
        return new RedirectResolution(StatusRemarkAction.class, "add_remark");
    }

    public RemarkManager getRemarkManager() {
        return remarkManager;
    }

    public void setRemarkManager(RemarkManager remarkManager) {
        this.remarkManager = remarkManager;
    }

    public StatusRemarkCode getRemark() {
        return remark;
    }

    public void setRemark(StatusRemarkCode remark) {
        this.remark = remark;
    }

    public StatusRemarkCodeDAO getStatusRemarkCodeDAO() {
        return statusRemarkCodeDAO;
    }

    public void setStatusRemarkCodeDAO(StatusRemarkCodeDAO statusRemarkCodeDAO) {
        this.statusRemarkCodeDAO = statusRemarkCodeDAO;
    }

    public List<StatusRemarkCode> getListofStatusRemark() {
        return listofStatusRemark;
    }

    public void setListofStatusRemark(List<StatusRemarkCode> listofStatusRemark) {
        this.listofStatusRemark = listofStatusRemark;
    }
}
